import java.awt.*;
import java.awt.event.*;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MainPanel extends JPanel implements MouseListener, Runnable {
	
	Thread timerthread;
	
	TracerGrid grid;
	BoundingBox cur = new BoundingBox(0, 0, 100, 100);
	
	Point end;
	
	Point mousepos = new Point(0, 0);
	Point mousepos_rel = new Point(0, 0);
	int width_rel, height_rel;
	int mousepress = 0;
	int mousestat = 1;
	
	int timer = 0;
	boolean trace = false;
	
	public void run() {
		while(true) {
			timer++;
			try { wait(1000); } catch(Exception e) { }
		}
	}
	
	public void gui() {
		mousepos = getMousePosition();
		if(mousestat == 0) {
			if(mousepos != null && mousepress == 0) {
				cur.centerpos(mousepos.x, mousepos.y);
			}
			if(mousepos != null && mousepress == 2) {
				Point rel = new Point(mousepos.x - mousepos_rel.x, mousepos.y - mousepos_rel.y);
				cur.scale(width_rel + rel.x*2, height_rel + rel.y*2);
			}
		} else if(mousestat == 1) {
			//if(mousepos != null && mousepress == 1)
			//	end = mousepos;
		}
		
		try { wait(50); } catch(Exception e) { }
	}
	
	public void paint(Graphics g) {
		
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;
		
		grid.mapBox();
		
		//Pathfinding
		if(end != null) {
			if(timer > 5000) {
				timer = 0;
				grid.tracePathAgent(end);
			}
		}
		
		grid.moveAgent(0.3);
		
		// Draw tiles
		grid.drawGrid(g2d);

		// Draw checked nodes
		//grid.drawChecked(g2d, false);
		
		// Draw path
		//grid.drawPath(g2d);
		
		// Draw unpassable nodes
		//grid.drawUnpassable(g2d);
		
		// Draw waypoints
		/*if(end != null) {
			g2d.setColor(Color.blue);
			grid.mapFill(g2d, end.x, end.y);
		}*/
		
		// Draw boxes
		grid.drawBox(g2d);
		
		// Draw dragged rect
		g2d.setColor(Color.black);
		if(mousestat == 0)
			cur.draw(g2d);
		
		// Draw trace
		grid.drawTrace(g2d);
		
		// Draw agent
		grid.drawAgent(g2d);
		
		Toolkit.getDefaultToolkit().sync();
        g.dispose();
        
        gui();
		repaint();
	}
	
	public MainPanel() {
		try {
			grid = new TracerGrid(0, 0, 1000, 800, 10);
			grid.centerposAgent(50, 50);
		} catch(Exception e) {
			System.out.println(e.getMessage());
			System.exit(-1);
		}
		addMouseListener(this);
		setBackground(Color.WHITE);
		setDoubleBuffered(true);
		timerthread = new Thread(this);
		timerthread.start();
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		int t = e.getButton();
		
		if(t == MouseEvent.BUTTON1) {
			mousepress = 1;
			if(mousestat == 0) {
				grid.addBox(new BoundingBox(cur));
			}
			if(mousestat == 1) {
				end = mousepos;
			}
		} else if(t == MouseEvent.BUTTON3) {
			mousestat++;
			if(mousestat > 1)
				mousestat = 0;
		} else if(t == MouseEvent.BUTTON2) {
			if(mousestat == 0) {
				width_rel = cur.w;
				height_rel = cur.h;
			}
			if(mousestat == 1) {
				grid.centerposAgent(mousepos.x, mousepos.y);
			}
			mousepos_rel = getMousePosition();
			mousepress = 2;
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		mousepress = 0;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) { }

	@Override
	public void mouseEntered(MouseEvent e) { }

	@Override
	public void mouseExited(MouseEvent e) {	}
	
}
